package com.pine.dcts.common;

import com.baomidou.mybatisplus.extension.service.IService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;

/**
 * @author xiaoyuan
 * @create 2019/12/1 19:12
 **/
@Validated
public class CURDHandler<S extends IService<T>,T> extends BaseHandler {

    @Autowired
    protected S service;


    /**
     * 新增
     */
    @PostMapping(value = "/add")
    public Result add(@Validated @RequestBody T entity) {
        return Result.success(service.save(entity));
    }

    /**
     * 删除
     */
    @PostMapping(value = "/delete")
    public Result<Boolean> delete(@NotBlank(message = "id不能为空") String ids) {
        return Result.success(service.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 修改
     */
    @PostMapping(value = "/update")
    public Result<Boolean> update(@Validated  @RequestBody  T entity) {
        return Result.success( service.updateById(entity));
    }


    /**
     * 获取
     */
    @GetMapping(value = "/get")
    public Result<T> get(@NotBlank(message = "id不能为空")  String id) {
        return Result.success(service.getById(id));
    }

}
